﻿/**************************************************************************\
    Copyright Microsoft Corporation. All Rights Reserved.
\**************************************************************************/

namespace Standard
{
    using System;
    using System.Windows.Interop;

    // Neat trick I wanted to write down for later.  Not currently using this:
    internal static class ZOrderHelper
    {
        public static void KeepWindowOnBottom(IntPtr hwnd)
        {
            Assert.IsNotDefault(hwnd);
            HwndSource.FromHwnd(hwnd).AddHook(_WndProc);
        }

        private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

        private static IntPtr _WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            WM message = (WM)msg;
            // Only tested this on Vista + Aero.
            // From looking at the messages I receive this seems complete, but sometimes I see
            // a flicker when releasing the left mouse button after a drag from the caption...
            switch (message)
            {
                case WM.SETCURSOR:
                case WM.CAPTURECHANGED:
                case WM.ACTIVATE:
                case WM.NCACTIVATE:
                case WM.NCMOUSELEAVE:
                case WM.SETFOCUS:
                case WM.KILLFOCUS:
                    NativeMethods.SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP.NOSIZE | SWP.NOMOVE | SWP.NOACTIVATE);
                    break;
            }
            //System.Diagnostics.Trace.WriteLine(message);
            return IntPtr.Zero;
        }
    }
}
